package com.boot.control;

import com.boot.bean.Student;
import com.boot.service.StudentService;
import com.sun.deploy.net.HttpResponse;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

@Controller
public class MyControl {

    @Autowired
    StudentService studentService;

    @RequestMapping("/find")
    @ResponseBody
    public Student test(Integer id) {
        return studentService.getName(id);
    }

    @RequestMapping("/find2")
    @ResponseBody
    public Student test2(Integer tid) {
        return studentService.getByTid(tid);
    }

    @RequestMapping("/url")
    @ResponseBody
    public void url(HttpServletResponse response, String no) {
        try {
            String u = null;
            if ("1".equals(no)) {
                u = "https://gitee.com/zng201021/javaagent#https://gitee.com/link?target=https%3A%2F%2Fwww.cnblogs.com%2FJoJo1021%2Fp%2F15933197.html";
            } else {
                u = "http://gitee.com/zng201021/javaagent#https://gitee.com/link?target=https%3A%2F%2Fwww.cnblogs.com%2FJoJo1021%2Fp%2F15933197.html";
            }
            URL url = new URL(u);
            URLConnection urlConnection = url.openConnection();
            urlConnection.connect();
//            InputStream inputStream = urlConnection.getInputStream();
//            ServletOutputStream outputStream = response.getOutputStream();
//            IOUtils.copy(inputStream, outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
